package com.xc.controller.backend;

import com.github.pagehelper.PageInfo;
import com.xc.common.ServerResponse;
import com.xc.service.IUserRechargeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

/**
 * @author mipha
 * @version 1.0
 * @date 2019/2/28 4:13 PM
 */
@Controller
@RequestMapping("/admin/recharge/")
@Slf4j
public class AdminRechargeController {

    @Autowired
    IUserRechargeService iUserRechargeService;

    /**
     *  管理后台 - 用户充值记录
     * @param agentId
     * @param userId
     * @param realName
     * @param state     0 已下单 1 支付成功 2支付失败
     * @param beginTime
     * @param endTime   时间查询的是 pay_time 支付时间 ，而不是下单时间
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(
                 @RequestParam(value = "agentId",required = false) Integer agentId
                ,@RequestParam(value = "userId",required = false) Integer userId
                ,@RequestParam(value = "realName",required = false) String realName
                ,@RequestParam(value = "state",required = false) Integer state
                ,@RequestParam(value = "beginTime",required = false) String beginTime
                ,@RequestParam(value = "endTime",required = false) String endTime
                ,@RequestParam(value = "pageNum", defaultValue = "1") int pageNum
                ,@RequestParam(value = "pageSize", defaultValue = "10") int pageSize
                ,HttpServletRequest request){
        return iUserRechargeService.listByAdmin(agentId,userId,realName
                ,state,beginTime,endTime,request,pageNum,pageSize);
    }

    /**
     *  修改充值订单的状态
     * @param chargeId  充值订单ID
     * @param state     修改状态 1 充值成功 2充值失败
     * @return
     */
    @RequestMapping("updateState.do")
    @ResponseBody
    public ServerResponse updateState(@RequestParam(value = "chargeId",required = false) Integer chargeId
            ,@RequestParam(value = "state",required = false) Integer state){
        ServerResponse serverResponse = null;
        try {
            serverResponse = iUserRechargeService.updateState(chargeId,state);
        }catch (Exception e){
            log.error("admin修改充值订单状态出错 ，异常 = {}" , e);
        }
        return serverResponse;
    }


    /**
     *  生成订单的接口
     *  用户每次转账的金额可能不一致，如果在前端没有生成订单或者订单已过有效期
     *  后台手动生成订单并到账金额
     * @param userId    用户id
     * @param state     0 下单    1 成功（成功会增加资金） 2 失败订单
     * @param amt       金额
     * @return
     */
    @RequestMapping("createOrder.do")
    @ResponseBody
    public ServerResponse createOrder(@RequestParam(value = "userId",required = false) Integer userId
          ,@RequestParam(value = "state",required = false) Integer state
            ,@RequestParam(value = "amt",required = false) Integer amt){
        return iUserRechargeService.createOrder(userId,state,amt);
    }

    /**
     *  删除充值记录
     * @param cId
     * @return
     */
    @RequestMapping("del.do")
    @ResponseBody
    public ServerResponse del(@RequestParam(value = "cId") Integer cId){
        return iUserRechargeService.del(cId);
    }

}
